package co.edu.unal.research.contentprovider.generator;

import co.edu.unal.research.contentprovider.contentProviderDsl.Configuration;
import co.edu.unal.research.contentprovider.contentProviderDsl.Table;
import co.edu.unal.research.contentprovider.contentProviderDsl.TableColumn;
import co.edu.unal.research.contentprovider.extensions.TableExtensions;
import com.google.inject.Inject;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.generator.IGenerator;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.IteratorExtensions;
import org.eclipse.xtext.xbase.lib.StringExtensions;

@SuppressWarnings("all")
public class ContentProviderDslGenerator implements IGenerator {
  @Inject
  private IQualifiedNameProvider _iQualifiedNameProvider;
  
  @Inject
  private TableExtensions _tableExtensions;
  
  public void doGenerate(final Resource resource, final IFileSystemAccess fsa) {
      Configuration configuration = ((Configuration) null);
      TreeIterator<EObject> _allContents = resource.getAllContents();
      Iterable<EObject> _iterable = IteratorExtensions.<EObject>toIterable(_allContents);
      Iterable<Configuration> _filter = IterableExtensions.<Configuration>filter(_iterable, co.edu.unal.research.contentprovider.contentProviderDsl.Configuration.class);
      for (final Configuration e : _filter) {
        configuration = e;
      }
      TreeIterator<EObject> _allContents_1 = resource.getAllContents();
      Iterable<EObject> _iterable_1 = IteratorExtensions.<EObject>toIterable(_allContents_1);
      Iterable<Table> _filter_1 = IterableExtensions.<Table>filter(_iterable_1, co.edu.unal.research.contentprovider.contentProviderDsl.Table.class);
      for (final Table e_1 : _filter_1) {
        QualifiedName _fullyQualifiedName = this._iQualifiedNameProvider.getFullyQualifiedName(e_1);
        String _string = _fullyQualifiedName.toString();
        String _operator_plus = StringExtensions.operator_plus(_string, ".java");
        CharSequence _generate = this.generate(e_1, configuration);
        fsa.generateFile(_operator_plus, _generate);
      }
  }
  
  public CharSequence generate(final Table table, final Configuration configuration) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.newLine();
    _builder.append("package ");
    String _packageName = configuration.getPackageName();
    _builder.append(_packageName, "");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("import java.util.*;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("public class ");
    String _name = table.getName();
    _builder.append(_name, "");
    _builder.append("Table {");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public static final String ");
    String _tableNameConstant = this._tableExtensions.getTableNameConstant(table);
    _builder.append(_tableNameConstant, "	");
    _builder.append(" = \"");
    String _name_1 = table.getName();
    _builder.append(_name_1, "	");
    _builder.append("\";");
    _builder.newLineIfNotEmpty();
    {
      EList<TableColumn> _columns = table.getColumns();
      for(final TableColumn column : _columns) {
        _builder.append("\t");
        _builder.append("public static final String ");
        String _columnNameConstant = this._tableExtensions.getColumnNameConstant(column);
        _builder.append(_columnNameConstant, "	");
        _builder.append(" = \"");
        String _name_2 = column.getName();
        _builder.append(_name_2, "	");
        _builder.append("\"");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("private static final String DATABASE_CREATE = \"create table \" ");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("+ TABLE_TODO");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("+ \"(\"");
    _builder.newLine();
    {
      EList<TableColumn> _columns_1 = table.getColumns();
      for(final TableColumn column_1 : _columns_1) {
        _builder.append("\t");
        _builder.append("+ ");
        String _columnNameConstant_1 = this._tableExtensions.getColumnNameConstant(column_1);
        _builder.append(_columnNameConstant_1, "	");
        _builder.append(" + \" ");
        String _columnAdditionalFeatures = this._tableExtensions.getColumnAdditionalFeatures(column_1);
        _builder.append(_columnAdditionalFeatures, "	");
        _builder.append(" \"");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.append("\t");
    _builder.append("+ \");\";");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public static void onCreate(SQLiteDatabase database) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("database.execSQL(DATABASE_CREATE);");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public static void onUpgrade(SQLiteDatabase database, int oldVersion,");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("int newVersion) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("Log.w(TodoTable.class.getName(), \"Upgrading database from version \"");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("+ oldVersion + \" to \" + newVersion");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("+ \", which will destroy all old data\");");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("database.execSQL(\"DROP TABLE IF EXISTS \" + TABLE_TODO);");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("onCreate(database);");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
}
